--Reference function library for the shield domes. Needs updating for Efficiency
--This library is seperate for maintenance purposes

function CDamage_checkshieldintersect(origin, vector, player, shield)
	local d = vector:GetNormal()
	local p0 = origin
	local pc = shield:GetPos()
	local r = shield:GetRadius()
	local a = d:DotProduct(d)
	local b = (2*d):DotProduct(p0 - pc)
	local c = (p0 - pc):DotProduct(p0 - pc)-(r^2)
	if (((b^2) - (4*a*c)) >= 0) then
		if ((origin - pc):Length() < r) then --Needs to compared without using length function (COMPUTATIONALLY GODDAMN EXPENSIVE SQUARE ROOT IS EXPENSIVE!)
			return false --Ray originated inside the provided shield, and therefore doesn't intersect
		else
			return true --Ray intersects provided shield
		end
	end
end

function CDamage_shieldintersectdistance(origin, direction, shield)
	local d = direction:GetNormal()
	local p0 = origin
	local pc = shield:GetPos()
	local r = shield:GetRadius()
	local a = d:DotProduct(d)
	local b = (2*d):DotProduct(p0 - pc)
	local c = (p0 - pc):DotProduct(p0 - pc)-(r^2)
	local upper = (-b + math.sqrt(b^2 - 4*a*c)) / (2*a) --Since this is a final check and only getting called once. we don't care as much that there's a square root
	local lower = (-b - math.sqrt(b^2 - 4*a*c)) / (2*a) --Besides. This is about as expensive as a single vector length check. So it's all good
	return math.min(upper, lower)
end

function CDamage_shieldintersectpoint(origin, direction, shield)
	local distance = CDamage_shieldintersectdistance(origin, direction, shield)
	local intersectvector = direction:GetNormal()*distance
	return origin + intersectvector
end

function CDamage_shieldsurfacenormal(shield, point)
	return (point - shield:GetPos()):GetNormal()
end

function CDamage_Vector_Compare(vector1, vector2) --THESE ARE LOW-EFFICIENCY FUNCTIONS THAT NEED OPTIMISATION!
	local length1, length2
	length1 = vector1:Length()
	length2 = vector2:Length()
	if (length1 > length2) then
		return 1
	elseif (length1 == length2) then
		return 0
	else
		return -1
	end
end

function CDamage_Vector_Length_Compare(vector, length2)
	local length1 = vector:Length()
	if (length1 > length2) then
		return 1
	elseif (length1 == length2) then
		return 0
	else
		return -1
	end
end